﻿Public Class DRUGUSETEMPLATE
    Dim connect As CONDB = CONDB.NewConnection()
    'Dim connectlocal As conlocal = conlocal.NewConnection

    Dim PROVIDER_Check As New PROVIDERSCLASS

    Private Sub DRUGUSETEMPLATE_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        selectDRUGUSETEMPLATEGrid()

        DesignForm(Me)
        DesignGrid(DrugUseTemplateGrid)

        PROVIDER_Check.GETHOSEM(CInt(main.txtUserRequest.Text.Trim), Convert.ToString(Me.Tag))
    End Sub

    Public Sub selectDRUGUSETEMPLATEGrid()
        Dim objSelectDrugusetemplate As New DRUGUSETEMPLATECLASS
        Dim DRUGUSETEMPLATE As DataTable
        DRUGUSETEMPLATE = connect.GetTable(objSelectDrugusetemplate.selectDrglbltemplate_)
        DrugUseTemplateGrid.PrimaryGrid.DataSource = DRUGUSETEMPLATE

        filterDrugName()
        filterDrugUse()
        filterDrugTime1()
        filterDrugTime2()
        filterWarning1()
        filterWarning2()
    End Sub

    Public Sub clearForm()
        DRGTMPIDTxt.Text = ""

        drugNameTxt.Text = ""
        drugNameTxt.Tag = ""

        DUSENAMETxt.Text = ""
        DUSENAMETxt.Tag = ""

        DTNAME1Txt.Text = ""
        DTNAME1Txt.Tag = ""

        DTNAME2Txt.Text = ""
        DTNAME2Txt.Tag = ""

        WRNNAME1Txt.Text = ""
        WRNNAME1Txt.Tag = ""

        WRNNAME2Txt.Text = ""
        WRNNAME2Txt.Tag = ""

        DrugUseTemplateStatusChk.Checked = False
    End Sub

    '-------clear Text and Tag when Click--------------------

    Private Sub drugNameTxt_Click(sender As Object, e As EventArgs) Handles drugNameTxt.Click
        drugNameTxt.Text = ""
        drugNameTxt.Tag = ""
    End Sub

    Private Sub DUSENAMETxt_Click(sender As Object, e As EventArgs) Handles DUSENAMETxt.Click
        DUSENAMETxt.Text = ""
        DUSENAMETxt.Tag = ""
    End Sub

    Private Sub DTNAME1Txt_Click(sender As Object, e As EventArgs) Handles DTNAME1Txt.Click
        DTNAME1Txt.Text = ""
        DTNAME1Txt.Tag = ""
    End Sub

    Private Sub DTNAME2Txt_Click(sender As Object, e As EventArgs) Handles DTNAME2Txt.Click
        DTNAME2Txt.Text = ""
        DTNAME2Txt.Tag = ""
    End Sub

    Private Sub WRNNAME1Txt_Click(sender As Object, e As EventArgs) Handles WRNNAME1Txt.Click
        WRNNAME1Txt.Text = ""
        WRNNAME1Txt.Tag = ""
    End Sub

    Private Sub WRNNAME2Txt_Click(sender As Object, e As EventArgs) Handles WRNNAME2Txt.Click
        WRNNAME2Txt.Text = ""
        WRNNAME2Txt.Tag = ""
    End Sub

    '------------หนูน้อยอะไรหว่า-------------
    Public Sub filterDrugName()
        Dim s As FILTERCLASS
        Dim sql As String = "SELECT CAST(drgcode AS CHAR(8)) AS DRGCODE, tradename, genericname FROM drugitem"
        s = New FILTERCLASS(drugNameTxt, sql, "รหัสยา,ชื่อทางการค้า,ชื่อสามัญ", "50,160,160", "1,1,1", "1,1,1")
        's.SetShowInDown = False
        s.SetShowBorder = True
    End Sub

    Public Sub filterDrugUse()
        Dim s As FILTERCLASS
        Dim sql As String = "SELECT CAST(ugid AS CHAR(8)) AS UGID, dusename, dusename_en FROM masdrguse"
        s = New FILTERCLASS(DUSENAMETxt, sql, "รหัสการใช้ยา,วิธีใช้ยา(ไทย),วิธีใช้ยา(อังกฤษ)", "50,160,160", "1,1,1", "1,1,1")
        's.SetShowInDown = False
        s.SetShowBorder = True
    End Sub

    Public Sub filterDrugTime1()
        Dim s As FILTERCLASS
        Dim sql As String = "SELECT CAST(dtcode AS CHAR(8)) AS DTCODE, dtname, dtname_en FROM masdrgtime WHERE dtcode <> '" & DTNAME2Txt.Tag & "'"
        s = New FILTERCLASS(DTNAME1Txt, sql, "รหัสเวลาการใช้ยา,เวลาใช้ยา(ไทย),เวลาใช้ยา(อังกฤษ)", "50,160,160", "1,1,1", "1,1,1")
        's.SetShowInDown = False
        s.SetShowBorder = True
    End Sub

    Public Sub filterDrugTime2()
        Dim s As FILTERCLASS
        Dim sql As String = "SELECT CAST(dtcode AS CHAR(8)) AS DTCODE, dtname, dtname_en FROM masdrgtime WHERE dtcode <> '" & DTNAME1Txt.Tag & "'"
        s = New FILTERCLASS(DTNAME2Txt, sql, "รหัสเวลาการใช้ยา,เวลาใช้ยา(ไทย),เวลาใช้ยา(อังกฤษ)", "50,160,160", "1,1,1", "1,1,1")
        's.SetShowInDown = False
        s.SetShowBorder = True
    End Sub

    Public Sub filterWarning1()
        Dim s As FILTERCLASS
        Dim sql As String = "SELECT CAST(wrncod AS CHAR(8)) AS WRNCOD, wrnname, wrnname_en FROM masdrgwrn WHERE wrncod <> '" & WRNNAME2Txt.Tag & "'"
        s = New FILTERCLASS(WRNNAME1Txt, sql, "รหัสคำเตือน,คำเตือน(ไทย),คำเตือน(อังกฤษ)", "50,160,160", "1,1,1", "1,1,1")
        's.SetShowInDown = False
        s.SetShowBorder = True
    End Sub

    Public Sub filterWarning2()
        Dim s As FILTERCLASS
        Dim sql As String = "SELECT CAST(wrncod AS CHAR(8)) AS WRNCOD, wrnname, wrnname_en FROM masdrgwrn WHERE wrncod <> '" & WRNNAME1Txt.Tag & "'"
        s = New FILTERCLASS(WRNNAME2Txt, sql, "รหัสคำเตือน,คำเตือน(ไทย),คำเตือน(อังกฤษ)", "50,160,160", "1,1,1", "1,1,1")
        's.SetShowInDown = False
        s.SetShowBorder = True
    End Sub

    '------------text change event------------

    Private Sub drugNameTxt_TextChanged(sender As Object, e As EventArgs) Handles drugNameTxt.TextChanged
        filterDrugName()
    End Sub

    Private Sub DUSENAMETxt_TextChanged(sender As Object, e As EventArgs) Handles DUSENAMETxt.TextChanged
        filterDrugUse()
    End Sub

    Private Sub DTNAME1Txt_TextChanged(sender As Object, e As EventArgs) Handles DTNAME1Txt.TextChanged
        filterDrugTime1()
        filterDrugTime2()
    End Sub

    Private Sub DTNAME2Txt_TextChanged(sender As Object, e As EventArgs) Handles DTNAME2Txt.TextChanged
        filterDrugTime1()
        filterDrugTime2()
    End Sub

    Private Sub WRNNAME1Txt_TextChanged(sender As Object, e As EventArgs) Handles WRNNAME1Txt.TextChanged
        filterWarning1()
        filterWarning2()
    End Sub

    Private Sub WRNNAME2Txt_TextChanged(sender As Object, e As EventArgs) Handles WRNNAME2Txt.TextChanged
        filterWarning1()
        filterWarning2()
    End Sub

    '-------------grid click---------------

    Private Sub DrugUseTemplateGrid_CellClick(sender As Object, e As DevComponents.DotNetBar.SuperGrid.GridCellClickEventArgs) Handles DrugUseTemplateGrid.CellClick
        Dim grid As DevComponents.DotNetBar.SuperGrid.GridRow
        grid = e.GridPanel.ActiveRow

        Try
            DRGTMPIDTxt.Text = CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("รหัส template การใช้ยา").Value()
        Catch ex As Exception
            DRGTMPIDTxt.Text = ""
        End Try

        Try
            drugNameTxt.Text = CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("ชื่อทางการค้า").Value()
            drugNameTxt.Tag = CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("รหัสยา").Value()
        Catch ex As Exception
            drugNameTxt.Text = ""
            drugNameTxt.Tag = ""
        End Try

        Try
            DUSENAMETxt.Text = CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("วิธีการใช้ยา(ไทย)").Value()
            DUSENAMETxt.Tag = CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("รหัสการใช้ยา").Value()
        Catch ex As Exception
            DUSENAMETxt.Text = ""
            DUSENAMETxt.Tag = ""
        End Try

        Try
            DTNAME1Txt.Text = CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("เวลาในการใช้ยา(ไทย1)").Value()
            DTNAME1Txt.Tag = CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("รหัสเวลาการใช้ยา(1)").Value()
        Catch ex As Exception
            DTNAME1Txt.Text = ""
            DTNAME1Txt.Tag = ""
        End Try

        Try
            DTNAME2Txt.Text = CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("เวลาในการใช้ยา(ไทย2)").Value()
            DTNAME2Txt.Tag = CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("รหัสเวลาการใช้ยา(2)").Value()
        Catch ex As Exception
            DTNAME2Txt.Text = ""
            DTNAME2Txt.Tag = ""
        End Try

        Try
            WRNNAME1Txt.Text = CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("คำเตือน(ไทย1)").Value()
            WRNNAME1Txt.Tag = CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("รหัสคำเตือน(1)").Value()
        Catch ex As Exception
            WRNNAME1Txt.Text = ""
            WRNNAME1Txt.Tag = ""
        End Try

        Try
            WRNNAME2Txt.Text = CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("คำเตือน(ไทย2)").Value()
            WRNNAME2Txt.Tag = CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("รหัสคำเตือน(2)").Value()
        Catch ex As Exception
            WRNNAME2Txt.Text = ""
            WRNNAME2Txt.Tag = ""
        End Try

        If CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("สถานะการเปิดใช้งาน").Value() = True Then
            DrugUseTemplateStatusChk.Checked = True
        Else
            DrugUseTemplateStatusChk.Checked = False
        End If

        Dim objChkboxUpdate As New CLICKGRIDCHECKBOX
        objChkboxUpdate.clickChkboxUpdate("drglbltemplate", CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("สถานะการเปิดใช้งาน").Value(), "STATUS", "DRGTMPID", DRGTMPIDTxt.Text)
    End Sub

    Private Sub addCountry_Click(sender As Object, e As EventArgs) Handles addCountry.Click
        If main.PROVIDERPOLICIES.checklogin_user_ = True Then
            If PROVIDER_Check.PADD_ = True Then
                If drugNameTxt.Text.ToString.Length > 0 And DUSENAMETxt.Text.Length > 0 Then
                    'connectlocal = conlocal.NewConnection
                    Dim objInsertData As New DRUGUSETEMPLATECLASS
                    objInsertData.DRGCODE_ = drugNameTxt.Tag
                    objInsertData.USEID_ = DUSENAMETxt.Tag
                    objInsertData.TIMEID1_ = DTNAME1Txt.Tag
                    objInsertData.TIMEID2_ = DTNAME2Txt.Tag
                    objInsertData.WRNID1_ = WRNNAME1Txt.Tag
                    objInsertData.WRNID2_ = WRNNAME2Txt.Tag
                    objInsertData.STATUS_ = DrugUseTemplateStatusChk.Checked

                    If objInsertData.insertDrugUseTemplateClass() = True Then
                        selectDRUGUSETEMPLATEGrid()
                        clearForm()
                    End If

                    connect.Dispose()
                Else
                    MsgBox("ท่านต้องทำการกรอกข้อมูลให้ครบถ้วนทุกช่องก่อนทำการเพิ่มข้อมูล")
                End If
            Else
                MsgBox(PROVIDER_Check.PADDMSG_)
            End If
        Else
            TOASTCLASS.gloadToastMSG(Me, main.PROVIDERPOLICIES.LOGINUSERMSG_)
            main.txtUserRequest.Focus()
        End If
    End Sub

    Private Sub editCountry_Click(sender As Object, e As EventArgs) Handles editCountry.Click
        If main.PROVIDERPOLICIES.checklogin_user_ = True Then
            If PROVIDER_Check.PUPD_ = True Then
                If DRGTMPIDTxt.Text.ToString.Length > 0 And drugNameTxt.Text.ToString.Length > 0 And DUSENAMETxt.Text.Length > 0 Then
                    Dim editStat As Integer
                    editStat = MsgBox("ท่านต้องการแก้ไขรูปแบบการใช้ยาและคำเตือนดังกล่าวใช่หรือไม่", MsgBoxStyle.OkCancel)

                    If editStat = 1 Then
                        'connectlocal = conlocal.NewConnection
                        Dim objEditData As New DRUGUSETEMPLATECLASS
                        objEditData.DRGTMPID_ = DRGTMPIDTxt.Text
                        objEditData.DRGCODE_ = drugNameTxt.Tag
                        objEditData.USEID_ = DUSENAMETxt.Tag
                        objEditData.TIMEID1_ = DTNAME1Txt.Tag
                        objEditData.TIMEID2_ = DTNAME2Txt.Tag
                        objEditData.WRNID1_ = WRNNAME1Txt.Tag
                        objEditData.WRNID2_ = WRNNAME2Txt.Tag
                        objEditData.STATUS_ = DrugUseTemplateStatusChk.Checked

                        If objEditData.editDrugUseTemplateClass() = True Then
                            selectDRUGUSETEMPLATEGrid()
                            clearForm()
                        End If

                        connect.Dispose()
                    End If
                Else
                    MsgBox("ท่านต้องทำการกรอกข้อมูลให้ครบถ้วนก่อนทำการแก้ไขข้อมูล")
                End If
            Else
                MsgBox(PROVIDER_Check.PUPDMSG_)
            End If
        Else
            TOASTCLASS.gloadToastMSG(Me, main.PROVIDERPOLICIES.LOGINUSERMSG_)
            main.txtUserRequest.Focus()
        End If
    End Sub

    Private Sub importExel_Click(sender As Object, e As EventArgs) Handles importExel.Click
        Dim nextform As New IMPORTEXCEL("drglbltemplate")
        nextform.ShowDialog()
    End Sub

    Private Sub exportExcel_Click(sender As Object, e As EventArgs) Handles exportExcel.Click
        Dim nextform As New IMPORTEXCEL("drglbltemplate")
        nextform.ShowDialog()
    End Sub
End Class